/**
 * Copyright 2009 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * Response entity data model.  A response is part of a discussion and contains the content of the discussion.
 * @author Adam
 * @version $Id: Response.java 24 2009-05-15 04:38:19Z a.ruggles $
 * 
 * Created on Feb 8, 2009 at 10:36:12 PM 
 */
@javax.persistence.Entity @Table(name = "sapien_responses")
@NamedQueries({
	@NamedQuery(name = "response.getCountDiscussion",
			query = "SELECT COUNT(r) FROM com.google.code.sapien.model.Response r WHERE r.discussion = :discussion")
})
public class Response implements Comparable<Response>, Serializable {
	/**
	 * Serial Version UID.
	 */
	private static final long serialVersionUID = 8755009761687291609L;

	/**
     * The Body/Text of the message.
     */
    private String body;

    /**
     * The date and time the post was created.
     */
    private Date created;

    /**
     * The User that created this post.
     */
    private User creator;

    /**
     * IP of the user that created the message.
     */
    private String creatorIpAddress;

    /**
     * The discussion this message is for.
     */
    private Discussion discussion;

    /**
     * Unique identifier.
     */
    private Long id;

    /**
     * The date and time the message was last modified.
     */
    private Date modified;

	/**
     * The version for optimistic concurrency control.
     */
    private Integer version;

	/**
	 * {@inheritDoc}
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	public int compareTo(final Response comparison) {
		int value = -1;
		if (this.created != null && comparison != null && comparison.getCreated() != null) {
			value = this.created.compareTo(comparison.getCreated());
		}
		return value;
	}

	/**
	 * {@inheritDoc}
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (!(obj instanceof Response)) {
			return false;
		}
		final Response rhs = (Response) obj;
		return new EqualsBuilder()
			.append(body, rhs.getBody())
			.append(created, rhs.getCreated())
			.isEquals();
	}

	/**
	 * Returns body.
	 * @return the body.
	 */
	@Lob @Basic(optional = false)
	public String getBody() {
		return body;
	}

	/**
	 * Returns created.
	 * @return the created.
	 */
	@Basic(optional = false) @Temporal(TemporalType.TIMESTAMP)
	public Date getCreated() {
		return created;
	}

	/**
	 * Returns creator.
	 * @return the creator.
	 */
	@ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "creator_id")
	public User getCreator() {
		return creator;
	}

	/**
	 * Returns creatorIpAddress.
	 * @return the creatorIpAddress.
	 */
	@Column(length = 40)
	public String getCreatorIpAddress() {
		return creatorIpAddress;
	}

	/**
	 * Returns discussion.
	 * @return the discussion.
	 */
	@ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "discussion_id")
	public Discussion getDiscussion() {
		return discussion;
	}

	/**
	 * Returns id.
	 * @return the id.
	 */
	@Id @GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	/**
	 * Returns modified.
	 * @return the modified.
	 */
	@Temporal(TemporalType.TIMESTAMP)
	public Date getModified() {
		return modified;
	}

	/**
	 * Returns version.
	 * @return the version.
	 */
	@Version
	public Integer getVersion() {
		return version;
	}

	/**
	 * {@inheritDoc}
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return new HashCodeBuilder(31, 37)
			.append(body)
			.append(created)
			.toHashCode();
	}

	/**
	 * Sets body.
	 * @param body the body to set.
	 */
	public void setBody(final String body) {
		this.body = body;
	}

	/**
	 * Sets created.
	 * @param created the created to set.
	 */
	public void setCreated(final Date created) {
		this.created = created;
	}

	/**
	 * Sets creator.
	 * @param creator the creator to set.
	 */
	public void setCreator(final User creator) {
		this.creator = creator;
	}

	/**
	 * Sets creatorIpAddress.
	 * @param creatorIpAddress the creatorIpAddress to set.
	 */
	public void setCreatorIpAddress(final String creatorIpAddress) {
		this.creatorIpAddress = creatorIpAddress;
	}

	/**
	 * Sets discussion.
	 * @param discussion the discussion to set.
	 */
	public void setDiscussion(final Discussion discussion) {
		this.discussion = discussion;
	}

	/**
	 * Sets id.
	 * @param id the id to set.
	 */
	public void setId(final Long id) {
		this.id = id;
	}

	/**
	 * Sets modified.
	 * @param modified the modified to set.
	 */
	public void setModified(final Date modified) {
		this.modified = modified;
	}

	/**
	 * Sets version.
	 * @param version the version to set.
	 */
	public void setVersion(final Integer version) {
		this.version = version;
	}

	/**
	 * {@inheritDoc}
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return new ToStringBuilder(this)
			.append("id", id)
			.append("created", created)
			.append("body", body)
			.append("modified", modified)
			.append("IP", creatorIpAddress)
			.append("version", version)
			.toString();
	}
}
